package com.shujia.scala

import scala.io.Source

object Demo27ClassNum {
  def main(args: Array[String]): Unit = {
    /**
     *
     * 统计每个班级的人数
     */

    //1、读取学生表
    val lines: List[String] = Source.fromFile("data/students.txt").getLines().toList

    //2、取出班级
    val clazzList: List[String] = lines.map((line: String) => {
      val split: Array[String] = line.split(",")
      split.last
    })

    //3、按照班级进行分组
    val groupBy: Map[String, List[String]] = clazzList.groupBy((clazz: String) => clazz)


    //4、统计班级的人数
   val clazzNum: Map[String, Int] =  groupBy.map((kv: (String, List[String])) => {
      val clazz: String = kv._1
      val list: List[String] = kv._2
      //计算班级的人数
      val num: Int = list.length
      (clazz, num)
    })

    clazzNum.foreach(println)

  }

}
